package com.ruoyi.project.hcy.task.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.hcy.administrations.domain.Branch;
import com.ruoyi.project.hcy.administrations.domain.Crew;
import com.ruoyi.project.hcy.task.domain.Task;
import com.ruoyi.project.hcy.task.mapper.TaskMapper;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 任务信息 服务层处理
 */
@Service
public class TaskServiceImpl implements ITaskService {

    @Autowired
    TaskMapper taskMapper;

    /**
     * 查询任务信息集合
     *
     * @param task 任务信息查询条件
     * @return
     */
    @Override
    public List<Task> selectTaskList(Task task) {
        return taskMapper.selectTaskList(task);
    }

    /**
     * 校验任务标题
     *
     * @param task 任务信息
     * @return
     */
    @Override
    public String checkTaskTitleUnique(Task task) {
        Long taskId = StringUtils.isNull(task.getTaskId()) ? -1L : task.getTaskId();
        Task info = taskMapper.checkTaskTitleUnique(task.getTaskTitle());
        if (StringUtils.isNotNull(info) && info.getTaskId().longValue() != taskId.longValue()) {

            return UserConstants.POST_NAME_NOT_UNIQUE;
        }
        return UserConstants.POST_NAME_UNIQUE;
    }

    /**
     * 保存任务信息
     *
     * @param task 任务信息
     * @return
     */
    @Override
    public int insertTask(Task task, Long executorId, String taskStartTimeStr, String taskEndTimeStr) {
        Date taskStartTime = strToDate(taskStartTimeStr);
        Date taskEndTime = strToDate(taskEndTimeStr);
        System.out.println(taskStartTime);
        User creatorUser = new User();
        creatorUser.setUserId(ShiroUtils.getUserId());
        Crew executorCrew = new Crew();
        executorCrew.setCrewId(executorId);
        task.setCreatorUser(creatorUser);
        task.setExecutorCrew(executorCrew);
        task.setTaskEndTime(taskEndTime);
        task.setTaskStartTime(taskStartTime);
        return taskMapper.insertTask(task);
    }

    /**
     * 根据部门拿到员工
     *
     * @return
     */
    @Override
    public List<Crew> selectCrews(String deptName) {
        return taskMapper.selectCrews(deptName);
    }

    @Override
    public List<Branch> selectBranchs() {
        return taskMapper.selectBranchs();
    }

    @Override
    public int deleteTaskByIds(String ids) {
        Long[] taskIds = Convert.toLongArray(ids);
        return taskMapper.deleteTaskByIds(taskIds);
    }

    @Override
    public Task selectTaskById(Long taskId) {
        return taskMapper.selectTaskById(taskId);
    }

    @Override
    public int updateTask(Task task, Long executorId) {
        Crew executorCrew = new Crew();
        executorCrew.setCrewId(executorId);
        task.setExecutorCrew(executorCrew);
        return taskMapper.updateTask(task);
    }

    @Override
    public int taskSubmit(Task task) {
        return taskMapper.taskSubmit(task);
    }

    @Override
    public List<Task> selectTaskList2(Task task) {
        return taskMapper.selectTaskList2(task);
    }

    private Date strToDate(String dateFormatterStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(dateFormatterStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
